import { http } from "@/utils/http";

export type UserResult = {
  errCode: number;
  errMsg: string;
  errDlt: string;
  data: {
    adminAccount: string;
    adminToken: string;
    nickname: string;
    faceURL: Array<string>;
    level: Array<string>;
    adminUserID: string;
    imUserID: string;
    imToken: Date;
    /** 当前登录用户的角色 */
    roles?: Array<string>;
    /** 按钮级别权限 */
    permissions?: Array<string>;
    /** 用于调用刷新`adminToken`的接口时所需的`token` */
    refreshToken?: string;
    /** `adminToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires?: Date;
  };
};
export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    adminToken: string;
    /** 用于调用刷新`adminToken`的接口时所需的`token` */
    refreshToken: string;
    /** `adminToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type UserInfo = {
  adminAccount: string;
  adminToken: string;
  nickname: string;
  faceURL: Array<string>;
  level: Array<string>;
  adminUserID: string;
  imUserID: string;
  imToken: Date;
  /** 头像 */
  avatar?: string;
  /** 用户名 */
  username?: string;
  email?: string;
  /** 联系电话 */
  phone?: string;
  /** 简介 */
  description?: string;
};

export type UserInfoResult = {
  errCode: number;
  errMsg: string;
  errDlt: string;
  data: UserInfo;
};

type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/im/account/login", { data });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

/** 账户设置-个人信息-获取 */
export const getMine = () => {
  return http.request<UserInfoResult>("post", "/im/account/info", { data: {} });
};

/** 账户设置-个人信息-更新*/
export const updataMine = (data?: object) => {
  return http.request<UserInfoResult>("post", "/im/account/update", { data });
};

/** 账户设置-个人信息-更新*/
export const changePassword = (data?: object) => {
  return http.request<UserInfoResult>("post", "/im/account/change_password", {
    data
  });
};
/** 账户设置-个人安全日志 */
export const getMineLogs = (data?: object) => {
  return http.request<ResultTable>("get", "/mine-logs", { data });
};
